

<template>
  <div class="shopcar-container">
    <div class="goods-list" v-for="item in CartArr" :key="item.id">
      <!-- 商品列表项区域 -->
      <div class="mui-card">
        <div class="mui-card-content">
          <div class="mui-card-content-inner">
            <mt-switch
              v-model="getGoodsSelected[item.id]"
              @change="selectedChanged(item.id,getGoodsSelected[item.id])"
            ></mt-switch>
            <img src="../../image2/01.jpg">
            <div class="info1">
              <h1>{{ '商品' }}</h1>
              <p>
                <span class="price">￥{{ item.price }}</span>
                <!-- 改变时传过去id 以及改变的值 -->
                <numbox :initcount="getGoodCount[item.id]" :id="item.id"></numbox>
                <!-- 问题：如何从购物车中获取商品的数量呢 -->
                <!-- 1. 我们可以先创建一个 空对象，然后循环购物车中所有商品的数据， 把 当前循环这条商品的 Id， 作为 对象 的 属性名，count值作为 对象的 属性值，这样，当把所有的商品循环一遍，就会得到一个对象： { 88: 2, 89: 1, 90: 4 } -->
                <a href="#">删除</a>
              </p>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 结算区域 -->
    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-content-inner jiesuan">
          <div class="left">
            <p>总计（不含运费）</p>
            <p>
              已勾选商品
              <span class="red">{{getCountPrice.totalCount}}</span> 件， 总价
              <span class="red">￥{{getCountPrice.totalPrice}}</span>
            </p>
          </div>
          <mt-button type="danger">去结算</mt-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState, mapGetters } from "vuex";
import numbox from "../together/number.vue";
export default {
  data() {
    return {};
  },
  methods: {
     selectedChanged(id, val) {
      // 每当点击开关，把最新的 快关状态，同步到 store 中
      // console.log(id + " --- " + val);
      this.$store.commit("updateGoodsSelected", { id, ballFlag: val });
    },
  },
  computed: {
    ...mapState(["CartArr"]),
    ...mapGetters(["getGoodCount", "getGoodsSelected" , "getCountPrice"])
  },
  components: {
    numbox
  }
};
</script>

<style lang="scss" scoped>
.shopcar-container {
  background-color: #eee;
  overflow: hidden;
  margin-top: 47px;
  .goods-list {
    .mui-card-content-inner {
      display: flex;
      align-items: center;
    }
    img {
      width: 60px;
    }
    h1 {
      font-size: 13px;
    }
    .info1 {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      .price {
        color: red;
        font-weight: bold;
      }
    }
  }
  .jiesuan {
    display: flex;
    justify-content: space-between;
    align-items: center;
    .red {
      color: red;
      font-weight: bold;
      font-size: 16px;
    }
  }
}
</style>
